package cn.yshujia.utils;

import org.springframework.util.ObjectUtils;

import java.util.Map;
import java.util.Set;

/**
 * @author YShuJia
 * @create 2024/10/29
 * @description ParamUtils 构建查询 SQL
 */

public class ParamUtils {
	
	/**
	 * @param map: key 为数据库字段，value为查询参数
	 *             [map] * @return String last sql
	 * @author YShuJia
	 * @description 构建模糊查询SQL
	 * @create 2024/10/29 13:35
	 */
	public static String createLikeSql(Map<String, Object> map) {
		if (ObjectUtils.isEmpty(map)) {
			return "and 1=1";
		}
		StringBuilder lastSql = new StringBuilder();
		lastSql.append("and ");
		// 遍历 map 获取参数
		Set<String> set = map.keySet();
		for (String key : set) {
			Object value = map.get(key);
			lastSql.append(key).append(" like '%").append(value).append("%' and");
		}
		
		lastSql.append(" 1=1");
		return lastSql.toString();
	}
}
